<?php
/**
 * This file contains the class RegisterEmailDB which provides
 * all of the database requirements for the register email page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
/**
 * This class provides access to the database for RegisterEmailDB
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
class RegisterEmailDB
{
    /**
      * Provides access to the database
     *
     * @var Db
     */
    private $_db;
    /**
     * Create a new instance of this class
     * 
     * @return void
     * @param Db &$db
     */
    function __construct(&$db)
    {
        $this->_db = $db;
    }
    /**
     * Escape the provided value
     *
     * @return string
     * @param mixed $value
     */
    function escape($value)
    {
        $db = $this->_db;
        return $db->escape($value);
    }
    /**
     * Check if a user account already exists
     *
     * @return int
     * @param string $email
     */
    function getUserIdByEmail($email)
    {
        $db = $this->_db;
        $userId = 0;
        $sql = <<< EOT
            SELECT ID
            FROM Users
            WHERE
                Username = '%s' OR
                Email = '%s'
EOT;
        $sql = sprintf($sql,
            $db->escape($email),
            $db->escape($email)
        );
        $result = $db->query($sql);
        if ($db->rowCount($result) > 0) {
            $row = $db->fetch($result);
            $userId = intval($row['ID']);
        }
        return $userId;
    }
    /**
     * Add a user to the database
     *
     * @return int
     * @param string $email
     */
    function addUser($email) {
        $db = $this->_db;
        $userId = 0;
        $sql = <<< EOT
            INSERT INTO Users
                SET
                    Username = '%s',
                    Email = '%s'
EOT;
        $sql = sprintf($sql, $db->escape($email), $db->escape($email));
        $result = $db->query($sql);
        if ($result === TRUE) {
            $userId = $db->insertID();
        }
        return $userId;
    }
}
?>